package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmDatasource;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmHttpService;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HttpServiceSetting;
import com.harmontronics.hanma.paas.paas.dto.input.HmHttpServiceCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmHttpServiceUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmDatasourceEnvResult;
import com.harmontronics.hanma.paas.paas.dto.output.HmDatasourceResult;
import com.harmontronics.hanma.paas.paas.dto.output.HmHttpServiceResult;
import com.harmontronics.hanma.paas.paas.dto.output.HttpServiceSettingResult;
import com.harmontronics.hanma.paas.paas.service.HmHttpServiceAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;


/**
 * 第三方服务
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-05-17 11:29:08
 */
@RestController
@RequestMapping("/api/hm-http-service")
@Api(tags = {"第三方服务"})
public class HmHttpServiceController {
    @Autowired
    private HmHttpServiceAppService hmHttpServiceappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmHttpServiceResult>> list() {
        List<HmHttpService> hmHttpServiceList = hmHttpServiceappService.getList();

        return ResponseEntity.ok(Convert.toList(HmHttpServiceResult.class, hmHttpServiceList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmHttpServiceResult>> list( @ApiParam("KEY") @RequestParam(required = false) String key,
                                                               @ApiParam("名称") @RequestParam(required = false) String name,
                                                               @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                              @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmHttpService> httpServicePageBase = hmHttpServiceappService.getList(key, name, pageNo, pageSize);
        PageBase<HmHttpServiceResult> resultPageBase = PageBase.copy(httpServicePageBase, HmHttpServiceResult.class);
        for (HmHttpServiceResult httpServiceResult : resultPageBase.getData()) {
            Optional<HmHttpService> httpService = httpServicePageBase.getData().stream().filter(e -> e.getId().longValue() == httpServiceResult.getId().longValue()).findFirst();
            httpService.ifPresent(hs -> httpServiceResult.setSettingList(Convert.toList(HttpServiceSettingResult.class, hs.getSettingList())));
        }
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmHttpServiceResult> info(@RequestParam("id") Long id) {
        HmHttpService hmHttpService = hmHttpServiceappService.findById(id);
        HmHttpServiceResult result = BeanUtil.toBean(hmHttpService, HmHttpServiceResult.class);
        List<HttpServiceSettingResult> settingResults = Convert.toList(HttpServiceSettingResult.class,hmHttpService.getSettingList());
        result.setSettingList(settingResults);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmHttpServiceCreateInput hmHttpServiceInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmHttpService hmHttpService = BeanUtil.toBean(hmHttpServiceInput, HmHttpService.class);
        hmHttpService.setSettingList(Convert.toList(HttpServiceSetting.class,hmHttpServiceInput.getSettingList()));
        hmHttpServiceappService.save(hmHttpService);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmHttpServiceUpdateInput hmHttpServiceInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmHttpService hmHttpService = BeanUtil.toBean(hmHttpServiceInput, HmHttpService.class);
        hmHttpServiceappService.update(hmHttpService);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmHttpServiceappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
